package com.atguigu.system.controller;


import com.atguigu.model.system.SysUser;
import com.atguigu.model.vo.SysUserQueryVo;
import com.atguigu.system.annotation.Log;
import com.atguigu.system.common.result.Result;
import com.atguigu.system.common.utils.MD5;
import com.atguigu.system.enums.BusinessType;
import com.atguigu.system.exception.CustomerException;
import com.atguigu.system.service.SysUserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author yqr
 * @since 2023-06-06
 */
@Api(tags = "用户管理")
@RestController
@RequestMapping("/admin/system/sysUser")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    //增加用户
    @Log(title = "用户管理", businessType = BusinessType.INSERT)
    @PreAuthorize("hasAuthority('bnt.sysUser.add')")
    @ApiOperation(value = "新增用户接口")
    @PostMapping("/add")
    public Result add(@RequestBody SysUser sysUser){

        //判断用户是否存在
        SysUser user = sysUserService.getByName(sysUser.getUsername());
        if (user != null){
            throw new CustomerException(201,"用户名已存在");
        }

        //对用户密码进行MD5加密
        String password = MD5.encrypt(sysUser.getPassword());
        sysUser.setPassword(password);
        sysUserService.save(sysUser);
        return Result.ok().message("用户新增成功!");
    }

    //删除用户
    @Log(title = "用户管理", businessType = BusinessType.DELETE)
    @PreAuthorize("hasAuthority('bnt.sysUser.remove')")
    @ApiOperation(value = "删除用户接口")
    @DeleteMapping("delete/{id}")
    public Result delete(@PathVariable Long id){
        sysUserService.removeById(id);
        return Result.ok().message("用户删除成功!");
    }

    //编辑用户
    @Log(title = "用户管理", businessType = BusinessType.SELECT)
    @PreAuthorize("hasAuthority('bnt.sysUser.list')")
    @ApiOperation(value = "编辑用户接口")
    @GetMapping("/get/{id}")
    public Result selectById(@PathVariable Long id){
        SysUser sysUser = sysUserService.getById(id);
        return Result.ok(sysUser);
    }

    //修改用户
    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    @PreAuthorize("hasAuthority('bnt.sysUser.update')")
    @ApiOperation(value = "修改用户接口")
    @PutMapping("/update")
    public Result update(@RequestBody SysUser sysUser){
        sysUserService.updateById(sysUser);
        return Result.ok().message("用户删除成功!");
    }

    //条件查询分页
    @Log(title = "用户管理", businessType = BusinessType.SELECT)
    @PreAuthorize("hasAuthority('bnt.sysUser.list')")
    @ApiOperation(value = "条件分页查询")
    @GetMapping("/{page}/{limit}")
    public Result selectByPage(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,

            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,

            @ApiParam(name = "userQueryVo", value = "查询对象", required = false)
            SysUserQueryVo sysUserQueryVo
    ){
        Page<SysUser> pageInfo = new Page<>(page,limit);
        IPage<SysUser> pageModel = sysUserService.getByConditionAndPage(pageInfo, sysUserQueryVo);
        return Result.ok(pageModel);

    }

    //更改用户状态
    @Log(title = "用户管理", businessType = BusinessType.STATUS)
    @PreAuthorize("hasAuthority('bnt.sysUser.update')")
    @ApiOperation(value = "更改用户状态")
    @PutMapping("/updateStatus/{id}/{status}")
    public Result updateStatus(@PathVariable String id, @PathVariable Integer status){
        sysUserService.updateStatus(id,status);
        return Result.ok().message("状态更改成功!");
    }
}

